﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BE = Skynet.BusinessEntities;
using WE = Skynet.WebFoundation.Model;
namespace Skynet.WebFoundation.Translators
{
    public  class CustomerTranslator : Infrastructure.Aspect.Services.Traslator.EntityMapperTranslator<BE.Customer, WE.Customer>
    {
        protected override WE.Customer BusinessToService(Infrastructure.Aspect.Services.Traslator.IEntityTranslatorService service, BE.Customer value)
        {
            var result = new WE.Customer();
            result.Id = value.Id;
            result.Name = value.Name;
            result.LastName = value.LastName;
            return result;  
        }

        protected override BE.Customer ServiceToBusiness(Infrastructure.Aspect.Services.Traslator.IEntityTranslatorService service, WE.Customer value)
        {
            var result = new BE.Customer();
            result.Id = value.Id;
            result.Name = value.Name;
            result.LastName = value.LastName;
            return result;  
        }
    }
    public class CustomersTranslator : Infrastructure.Aspect.Services.Traslator.EntityMapperTranslator<IEnumerable<BE.Customer>, IEnumerable<WE.Customer>>
    {
        public override bool CanTranslate(Type targetType, Type sourceType)
        {
            if (targetType.GetInterface("System.Collections.IEnumerable", false) != null
                && sourceType.GetInterface("System.Collections.IEnumerable", false) != null
                && targetType.IsGenericType == true && sourceType.IsGenericType == true)
            {
                var target = targetType.GetGenericArguments().ElementAt(0);
                var source = sourceType.GetGenericArguments().ElementAt(0);

                var res = (target == typeof(BE.Customer) && source == typeof(WE.Customer)) || (target == typeof(WE.Customer) && source == typeof(BE.Customer));
                return res;

            }
            return false;
        }
        protected override IEnumerable<WE.Customer> BusinessToService(Infrastructure.Aspect.Services.Traslator.IEntityTranslatorService service, IEnumerable<BE.Customer> value)
        {
            var res = new HashSet<WE.Customer>();
            foreach (var item in value)
            {
                var it = service.Translate<WE.Customer>(item);
                res.Add(it);
            }
            return res;
        }

        protected override IEnumerable<BE.Customer> ServiceToBusiness(Infrastructure.Aspect.Services.Traslator.IEntityTranslatorService service, IEnumerable<WE.Customer> value)
        {
            var res = new HashSet<BE.Customer>();
            foreach (var item in value)
            {
                var it = service.Translate<BE.Customer>(item);
                res.Add(it);
            }
            return res;
        }
    }
}
